package com.tiku.handler;



import com.alibaba.fastjson.JSON;
import com.tiku.common.ResultJson;
import com.tiku.utils.WebUtils;

import org.springframework.security.core.AuthenticationException;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Component
public class AuthenticationEntryPoint implements org.springframework.security.web.AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        System.err.println(authException.getMessage());
        if ("Full authentication is required to access this resource".equals(authException.getMessage())){
            ResultJson error = ResultJson.error("登录过期,请重新登录");
            String json = JSON.toJSONString(error);
            WebUtils.renderString(response,json);
            return;
        }
        ResultJson error = ResultJson.error(authException.getMessage());
        String json = JSON.toJSONString(error);
        WebUtils.renderString(response,json);
    }
}
